// packageC/voice_shop/my_voices/my_voices.js
var app = getApp();
// let innerAudioContext = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stop1: `${app.globalData.imgPath}stop1.png`,
    play1:  `${app.globalData.imgPath}play1.png`,
    songStop:false,
    selectedNum:null,
    sliderTime:0,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    this.init();
  },
  gotoDetail(e){
    console.log(e.currentTarget.dataset.info,'跳转到商品详情的id');
    console.log(e.currentTarget.dataset.type);
    let type = e.currentTarget.dataset.type;
    let sta = e.currentTarget.dataset.status;
    if(type == 0){
      wx.showToast({
        title: sta,
        icon:'none'
      });
    }else{
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + e.currentTarget.dataset.info,
      });
    }
   
  },
  init(){
    var that = this;
    this.innerAudioContext = wx.createInnerAudioContext();
    // innerAudioContext.autoplay = true
    // innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    this.innerAudioContext.onPlay(() => {
      console.log('开始播放');
      console.log(this.innerAudioContext.duration);
    });
    this.innerAudioContext.onTimeUpdate(() => {
      setTimeout(() => {
        let smun = (this.innerAudioContext.currentTime / this.innerAudioContext.duration) * 100;
        var slmin = Math.floor(smun);
        that.setData({
          sliderTime: slmin
        });
        if (Math.floor(this.innerAudioContext.duration) == Math.floor(this.innerAudioContext.currentTime)) {
          this.setData({
            songStop: true
          });
        }
      }, 1000);
    });
    this.innerAudioContext.onWaiting((res) =>{
      console.log('1111');
    });
    this.innerAudioContext.onError((res) => {
      console.log(res.errMsg);
      console.log(res.errCode);
    });
    this.innerAudioContext.onSeeking((res) => {
      console.log(res.errMsg);

    });
  },
  tapStatus(e){
    console.log(e.currentTarget.id);
    var eind = e.currentTarget.id;
    this.setData({
      src: this.data.voiceList[eind].has_one_voice.curriculum[0].url,
      songStop: false,
      selectedNum:eind
    });
    
    this.innerAudioContext.src = this.data.src;
    console.log(this.innerAudioContext.src); 
    this.innerAudioContext.play();

  },
  tapStatus_f(e){
    console.log(e.currentTarget.id);
    var eind = e.currentTarget.id;
    this.setData({
      songStop: true,
      selectedNum: eind
    });
    this.innerAudioContext.pause();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.innerAudioContext.destroy();
    this.setData({
      songStop:true
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    console.log(that.data.current_page);
    console.log(that.data.last_page);
    if (that.data.current_page < that.data.last_page) {
      wx.showLoading({
        title: '加载中',
        mask: true,
      });
      that.setData({
        page: that.data.page + 1
      });
      that.getNext(this.data.selected);
    } else {
      wx.showToast({
        title: '没有更多',
        icon: 'none'
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData(){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.voice-goods.api.voice-goods.getVoiceList");
    app._getNetWork({
      url: urlStr,
      data:{
        page:that.data.page
      },
      success: function (resdata) {
        var res = resdata.data;
        if(res.result == 1){
          var ingo = res.data.data;
          for(let i=0;i<ingo.length;i++){
            ingo[i].created_at = ingo[i].created_at.substring(0, 10);
          }
          that.setData({
            voiceList: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page
          });
        }
        console.log(res.data.data);
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getNext(){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.voice-goods.api.voice-goods.getVoiceList");
    app._getNetWork({
      url: urlStr,
      data: {
        page: that.data.page
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.current_page > res.data.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: '没有更多',
              icon: 'none'
            });
            that.setData({
              current_page: res.data.current_page,
              last_page: res.data.last_page
            });
            return;
          }else{
            wx.hideLoading();
            that.setData({
              voiceList: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            });
          }
        }else{
          console.log(res.msg);
        }
        console.log(res.data.data);
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});